
var image = require('../../..//utils/util.js');
const app = getApp();
Page({
  data: {
    allthing: app.globalData,
  },
  onLoad: function (options) {
    var that = this;
    var imageSize = image.image()
    console.log(imageSize)
    that.setData({
      imageWidth: imageSize.imageWidth,
      imageHeight: imageSize.imageHeight,
      chaWidth: imageSize.chaWidth,
      chaHeight: imageSize.chaHeight,
    })
    that.getAvatarUrl(that.data.allthing.userInfo.avatarUrl)
    that.getSpecialM()
    that.getBackground()
    that.getTouground()
    wx.showToast({
      title: '正在生成图片',
      icon: 'loading',
      duration: 10000,
    })
    setTimeout(function () {           //延迟执行，避免请求文件无效
      console.log("----Countdown----")
      that.createImg()
      wx.hideToast()
    }, 1000)
  },
  /**
   * 获取特定页面的小程序码
   */
  getSpecialM: function () {
    var that = this
    //请求获取小程序码
    wx.request({
      method: 'GET',
      url: 'https://www.a******_code.php?sid=' + that.data.userInfo.subject_id,//小程序有关token的api接口需要写在后台服务器端
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        wx.downloadFile({
          url: res.data.data.url,
          success: function (res) {
            that.setData({
              icon4: res.tempFilePath,
            })
          },
          fail: function () {
            console.log('fail')
          }
        })
      }
    })
  },
  /**
   * 获取头像
   */
  getAvatarUrl: function (avatarUrl) {
    var that = this
    //保存头像
    wx.downloadFile({
      url: avatarUrl,
      success: function (res) {
        that.setData({
          exam: res.tempFilePath,
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  /**
   * 获取背景
   */
  getBackground: function () {
    var that = this
    wx.downloadFile({
      url: 'https://app.c***answer/2.png',
      success: function (res) {
        that.setData({
          share: res.tempFilePath,
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  /**
   * 获取头像框
   */
  getTouground: function () {
    var that = this
    wx.downloadFile({
      url: 'https://app.ci1*******er/phot.png',
      success: function (res) {
        that.setData({
          phot: res.tempFilePath,
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  /**
   * 生成画布
   */
  createImg: function () {
    var that = this
    var ctx = wx.createCanvasContext('myCanvas');
    ctx.setFillStyle('White')
    ctx.fillRect(0, 0, 300, 400)
    ctx.drawImage(that.data.icon4, 115 + that.data.chaWidth / 2, 153 + that.data.chaHeight / 2, 92, 92)
    ctx.drawImage(that.data.share, 0, 0, that.data.imageWidth, that.data.imageHeight)
    ctx.drawImage(that.data.exam, 138 + that.data.chaWidth / 2, 10, 50, 50)
    ctx.drawImage(that.data.phot, 138 + that.data.chaWidth / 2, 10, 50, 50)
    ctx.draw();
  },
  savePic: function () {       //保存图片触发事件
    var that = this
    wx.canvasToTempFilePath({
      width: that.data.imageWidth,
      height: that.data.imageHeight,
      canvasId: 'myCanvas',
      success: function (res) {
        util.savePicToAlbum(res.tempFilePath)
      }
    })
    setTimeout(function () {
      console.log("----Countdown----")
      wx.redirectTo({
        url: '/pages/my/my/my',
      })
    }, 1000)

  }
})
